<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2017/4/3
 * Time: 16:53
 */

namespace app\home\controller;


class Huodong extends Base
{
    private $obj;
    public function _initialize()
    {
        parent::_initialize();
        $cate = cache('activityCate');
        $this->obj = model('activity');
        $this->assign('cate',$cate['cate']);
    }
    public function index(){

        $cate_id = input('param.cate_id/d',0);
        $cate_id && $where['cate_id'] = $cate_id;
        $where['status'] = 1;
        $where['city']   = $this->cityInfo['id'];
        $lists = $this->obj->where($where)->field('id,title,img,description,begin_time,end_time')->order('ordid asc,id desc')->paginate(16);
        $this->assign('cate_id',$cate_id);
        $this->assign('lists',$lists);
        $this->assign('pages',$lists->render());

        return $this->fetch();
    }
    public function detail(){
        $id = input('param.id/d',0);
        if($id){
            $info = $this->obj->where(['status'=>1,'id'=>$id])->find();
            if(!$info){
                return $this->fetch('public/404');
            }
            $this->obj->where(['id'=>$id])->setInc('hits');
            $this->assign('info',$info);
            $this->setSeo($info);
            $this->assign('prenews',$this->getPreNext($info['id'],$info['cate_id']));
            $this->assign('nextnews',$this->getPreNext($info['id'],$info['cate_id'],'next'));
        }else{
            return $this->fetch('public/404');
        }
        return $this->fetch();
    }
    /**
     * @param $id
     * @param $cate_id
     * @param string $type
     * @return string
     * 获取 上一篇，下一篇文章
     */
    private function getPreNext($id,$cate_id,$type='pre'){
        if($type == 'pre'){
            $map['id'] = ['lt',$id];
            $sort      = 'id desc';
        }else{
            $map['id'] = ['gt',$id];
            $sort      = 'id asc';
        }
        $map['status']  = 1;
        $map['cate_id'] = $cate_id;
        $map['create_time'] = ['lt',time()];
        $info           = $this->obj->where($map)->field('id,title')->order($sort)->find();
        if($info){
            $str = "<a href='".url('Huodong/detail',['id'=>$info['id']])."' title='".$info['title']."'>".$info['title']."</a>";
        }else{
            $str = '没有了';
        }
        return $str;
    }

}